﻿using DbControl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Common;
using Models;

namespace CentralMonitoring
{
    /// <summary>
    /// 油品信息管理窗体
    /// </summary>
    public partial class SensorsFrm : Form
    {
        public SensorsFrm()
        {
            InitializeComponent();
        }
        private void InitCombox()
        {
            comboBoxDevice.DataSource = OprSensor.GetSensorAll();
            comboBoxDevice.DisplayMember = "sensor_name";
            comboBoxDevice.ValueMember = "id";
            //if (comboBoxDevice.Items.Count > 0)
            //{
            //    if(!string.IsNullOrEmpty(session.m_currentDeviceId))
            //    {
            //        Device dv=OprSql.GetDeviceByDeviceId(session.m_currentDeviceId);
            //        if(dv!=null)
            //            comboBoxDevice.Text = dv.DeviceName;
            //        else
            //            comboBoxDevice.SelectedIndex = 0;
            //    }
            //    else
                    comboBoxDevice.SelectedIndex = 0;
            //}
            RefreshGrid("");
        }
        private void ReInitCombox()
        {
            comboBoxDevice.Items.Clear();
            comboBoxDevice.DataSource = OprSensor.GetSensorAll();
            comboBoxDevice.DisplayMember = "sensor_name";
            comboBoxDevice.ValueMember = "id";
            //if (comboBoxDevice.Items.Count > 0)
            //{
            //    if (!string.IsNullOrEmpty(session.m_currentDeviceId))
            //    {
            //        Device dv = OprSql.GetDeviceByDeviceId(session.m_currentDeviceId);
            //        comboBoxDevice.Text = dv.DeviceName;
            //    }
            //    else
                    comboBoxDevice.SelectedIndex = 0;
            //}
            RefreshGrid("");
        }
        private void SensorsFrm_Load(object sender, EventArgs e)
        {
            this.StatusLabel0.Text = string.Format("系统时间：{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            InitCombox();

            this.WindowState = FormWindowState.Maximized;
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            SensorEdit dlg = new SensorEdit();
            dlg.m_type =OprType.Add;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                RefreshGrid("");
            }
        }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)//dataGridView1.Rows.Count
            {
                int nID = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;//(int)dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["Id"].Value;
                SensorEdit dlg = new SensorEdit();
                dlg.m_type =OprType.Modify;
                dlg.m_Id = nID;
                dlg.m_beforName = dataGridView1.SelectedRows[0].Cells["sensor_name"].Value.ToString();
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    RefreshGrid("");
                }
            }
        }

        private void buttonDel_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                try
                {
                    if (MessageBox.Show("确定要删除该条数据吗？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) != DialogResult.OK)
                        return;
                    //int currenID = dataGridView1.SelectedRows.Count;                   
                    //int nId = (int)dataGridView1.Rows[dataGridView1.SelectedRows[currenID].Index].Cells["id"].Value;
                    int nId = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                    if(OprSql.DeviceDel(nId)>0)
                        InitCombox();
                    else
                        MessageBox.Show("删除失败！");
                }
                catch (Exception ex)
                {                   
                    MessageBox.Show("删除失败！");
                    lLogWrite.WriteLog(ex.ToString());                  
                }
            }
        }

        private void RefreshGrid(string name)
        {          
            DataTable dt = OprSensor.GetSensorByName(name);
            if (dt != null)
            {
                this.dataGridView1.DataSource = dt;
                dataGridView1.Columns["id"].Visible = false;
                dataGridView1.Columns["sensor_code"].HeaderText = "终端编码";
                dataGridView1.Columns["sensor_name"].HeaderText = "终端名称";
                dataGridView1.Columns["sensor_sn"].HeaderText = "物联卡";
                dataGridView1.Columns["gateway_id"].Visible=false;
                dataGridView1.Columns["sensor_type"].HeaderText = "终端型号";
                dataGridView1.Columns["sensor_mode"].HeaderText = "终端类型";
                dataGridView1.Columns["sensor_topic"].HeaderText = "发布";
                dataGridView1.Columns["sensor_subscribe"].HeaderText = "订阅";
                dataGridView1.Columns["flag"].HeaderText = "状态";
                dataGridView1.Columns["online_status"].HeaderText = "是否在线";
                dgSetView.SetFormatDataGridView(dataGridView1);
            }

        }

        private void buttonParaSet_Click(object sender, EventArgs e)
        {
            DeviceByOilParaSet dlg = new DeviceByOilParaSet();
            dlg.ShowDialog();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void buttonFind_Click(object sender, EventArgs e)
        {
            //if(comboBoxDevice.Items.Count>0)
            if(!string.IsNullOrEmpty(comboBoxDevice.Text))
                RefreshGrid(comboBoxDevice.Text);
            else
                RefreshGrid("");
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            StatusLabel0.Text = string.Format("系统时间：{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
        }

        private void toolStripStatusLabel1_Click(object sender, EventArgs e)
        {
            ServerEdit dlg=new ServerEdit();
            dlg.ShowDialog();
        }
        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e == null || e.Value == null || !(sender is DataGridView))
            {
                return;
            }
            DataGridView dgv = (DataGridView)sender;
            object originalValue = e.Value;
            int dValue = 0;
            if (dgv.Columns[e.ColumnIndex].DataPropertyName == "flag")
            {
                if (e.Value != null && int.TryParse(e.Value.ToString(), out dValue))
                    e.Value = dValue > 0 ? "启用" : "禁用";
            }
            if (dgv.Columns[e.ColumnIndex].DataPropertyName == "online_status")
            {
                if (e.Value != null && int.TryParse(e.Value.ToString(), out dValue))
                    e.Value = dValue > 0 ? "在线" : "离线";
            }
        }
    }
}
